<?php declare(strict_types=1);

/*
 * This file is part of Pusher.
 *
 * (c) Jetsung Chan <jetsungchan@gmail.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Pusher\Tests\Channels;

use PHPUnit\Framework\TestCase;
use Pusher\Channel\Pushsafer;
use Pusher\Message\PushsaferMessage;

class PushsaferTest extends TestCase
{
    private string $token = '';

    ## 50 条免费，故跳过测试
    private static bool $PASS = true;

    public function setUp(): void
    {
        $token = getenv('PushsaferToken');
        if ($token) {
            $this->token = $token;
        } else {
            self::$PASS = true;
        }
    }

    public function skipTest(string $func, bool $skip = false): void
    {
        if (self::$PASS || $skip) {
            $this->markTestSkipped("skip {$func}");
        }
    }

    public static function additionProvider(): array
    {
        return [
            [
                'Pusher通知标题一',
                'Pushsafer 测试。项目地址：https://github.com/idev-sig/pusher <strong>测试一下</strong>',
                0,
                2,
                3,
                '#FF0000',
                'a',
                'https://github.com/idev-sig/pusher',
                '链接标题',
                '',
            ],
            [
                'Pusher通知标题二',
                'Pushsafer 测试二。项目地址：https://github.com/idev-sig/pusher <strong>测试一下</strong>',
                2,
                1,
                181,
                '#0000FF',
                'a',
                'https://github.com/idev-sig/pusher',
                '链接标题二',
                '',
            ],
        ];
    }

    public static function additionProviderImage(): array
    {
        $base64 = '';

        return [
            [ $base64 ],
        ];
    }

    /**
     * @dataProvider additionProvider
     *
     * @return void
     */
    public function testCases(
        string $title,
        string $message,
        int $sound,
        int $vibration,
        int $icon,
        string $icon_color,
        string $device,
        string $url,
        string $url_title,
        string $picture,
    ): void {
        $this->skipTest(__METHOD__);

        $channel = new Pushsafer();
        $channel->setToken($this->token);

        $message = new PushsaferMessage($message, $title);
        $message->setSound($sound)
            ->setVibration($vibration)
            ->setIcon($icon)
            ->setIconColor($icon_color)
            ->setDevice($device)
            ->setURL($url)
            ->setUrlTitle($url_title)
            ->setPicture($picture);

        $channel->request($message);

        echo "\n";
        if (!$channel->getStatus()) {
            var_dump($channel->getContents());//, $channel->getContents());
        }
        $this->assertTrue($channel->getStatus());
    }

    /**
     * @dataProvider additionProviderImage
     *
     * @return void
     */
    public function testImageCases(string $picture): void
    {
        $this->skipTest(__METHOD__);

        $channel = new Pushsafer();
        $channel->setToken($this->token);

        $message = '<strong>这是一条图片消息，<a href="https://github.com/idev-sig/pusher">项目地址</a></strong>';
        $title = '这是一条图片消息';

        $message = new PushsaferMessage($message, $title);
        $message->setSound(50)
            ->setVibration(3)
            ->setIcon(50)
            ->setPicture($picture);

        $channel->request($message);

        echo "\n";
        if (!$channel->getStatus()) {
            var_dump($channel->getErrMessage());//, $channel->getContents());
        }
        $this->assertTrue($channel->getStatus());
    }
}
